// mine/rate/index.js
import Toast from '../../ui/toast/index'
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pic_url:app.base_url,
        originFiles: [
            {
              url: 'https://tdesign.gtimg.com/miniprogram/images/example4.png',
              name: 'uploaded1.png',
              type: 'image',
            },
            {
              url: 'https://tdesign.gtimg.com/miniprogram/images/example6.png',
              name: 'uploaded2.png',
              type: 'image',
            },
            {
              url: 'https://tdesign.gtimg.com/miniprogram/images/example5.png',
              name: 'uploaded1.png',
              type: 'image',
            },
          ],
          gridConfig: {
            column: 4,
            width: 160,
            height: 160,
          },
          config: {
            count: 1,
          },
          order_id:0,
          form:[],
          up_file:[],
          is_name:0,//匿名
          up_num:1,
    },

    //获取上传数量
    get_up_num(){
      app.http.request("index.php?r=api/index/upnum").then((res)=>{
        if(res.data.result==1){
          this.setData({
            up_num:res.data.data,
          });
        }
      });
    },

    handleSuccess(e) {
        let index = e.currentTarget.dataset.index;
        const { files } = e.detail;
            let pic = files.pop().url;
            wx.uploadFile({
              filePath: pic,
              formData:{token:wx.getStorageSync('token')},
              name: 'UpModel[up_image]',
              url: app.base_url+"web/index.php?r=user/upload/up",
              success:(res)=>{
                  res = JSON.parse(res.data);
                  if(res.result=='login_error'){
                      Toast({
                          selector:"#toast",
                          message:res.data,
                          context:this,
                      });
                  }
                  if(res.result==1){
                    this.data.form[index].pics.push(res.data);
                    let p ={};
                    p.url = res.data;
                    this.data.up_file[index].push(p);
                    this.setData({
                        up_file:this.data.up_file,
                    });
                  }else{
                      Toast({
                          selector:"#toast",
                          message:res.data,
                          context:this,
                      });
                  }
              },
              fail:(res)=>{
                  console.log(res);
                  Toast({
                      selector:"#toast",
                      message:res.errMsg,
                      context:this
                  });
              }
            })
        
      },
      handleRemove(e) {
        console.log(e);
        let file = e.detail.file.url;
        let index = e.currentTarget.dataset.index;
        let pics = this.data.form[index].pics;
        let pic_index=-1;
        for(let i=0;i<pics.length;i++){
          if(file==pics[i]){
            pic_index=i;
            
            break;
          }
        }
        if(pic_index>=0){
          app.login.request("index.php?r=user/rate/delpic",{pic:file}).then((res)=>{
            if(res.data.result==1){
              this.data.form[index].pics.splice(pic_index,1);
              this.data.up_file[index].splice(pic_index,1);
              this.setData({
                form:this.data.form,
                up_file:this.data.up_file,
              });
            }
          });
        }
        
      },
      handleClick(e) {
        console.log(e.detail.file);
      },

      //获取商品
      get_goods(){
        app.login.request("index.php?r=user/rate/list",{id:this.data.order_id}).then((res)=>{
            if(res.data.result==1){
                
                this.setData({
                    list:res.data.data,
                });
                this.data.form = [];
                for(let i=0;i<this.data.list.goods.length;i++){
                    let p={
                        pics:[],
                        content:'',
                        rate:2,
                        goods_id:this.data.list.goods[i].goods_id,
                        order_id:this.data.list.id,
                        attr:this.data.list.goods[i].attr,
                    };//表单
                    this.data.form.push(p);
                    let arr=[];
                    this.data.up_file.push(arr);
                }
                this.setData({
                  form:this.data.form,
                });
            }else{
                Toast({
                    id:'#toast',
                    message:res.data.data,
                    context:this,
                });
            }
        });
      },

      //评分
      change_rate(e){
        let index = e.currentTarget.dataset.index;
        let value = e.currentTarget.dataset.value;
        this.data.form[index].rate=value;
        this.setData({
          form:this.data.form,
        });
      },

      //获取内容
      change_inp(e){
        let index = e.currentTarget.dataset.index;
        let value = e.detail.value;
        console.log(value);
        this.data.form[index].content = value;
      },


      //提交
      post(){
        app.login.request("index.php?r=user/rate/addrate",{form:this.data.form,order:this.data.order_id,is_name:this.data.is_name}).then((res)=>{
          console.log(res);
          if(res.data.result==1){
            Toast({
              selector:"#toast",
              context:this,
              message:res.data.data,
            });
            wx.navigateBack({
              delta:-1,
            })
          }else{
            Toast({
              selector:"#toast",
              context:this,
              message:res.data.data,
            });
          }
        });
      },

      //匿名
      onChange(e){
        let value = e.detail.value;
        if(value.length>0){
          this.data.is_name=1;
        }else{
          this.data.is_name=0;
        }
      },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if(options!={}){
            this.data.order_id = options.id;
            this.get_goods();
            this.get_up_num();
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})